 # --*--coding；utf-8--*--

import json
import os
import sys


def hostname():
	sys = os.name
	if sys == 'nt':
		hostname = os.getenv('computername')
		return hostname
	elif sys == 'posix':
		host = os.popen('echo $HOSTNAME')
		try:
			hostname = host.read()
			return hostname
		finally:
			host.close()
	else:
		return 'Unkwon hostname'


class Json_file():

	if (hostname()) == 'DESKTOP-424AROC':  #开发环境
		file_url = 'C:\\Users\\mayn\\Desktop\\program_manage\\'
	else:
		file_url = '/home/Program_manage/'

	#通过用户名创建一个json文件
	def crate_file(self,username):
		new_dict =	{
			"username":username,
			"project": []
		}
		with open(self.file_url+username+".json","w") as f:
			json.dump(new_dict,f)

	# 添加数据到json文件
	def add_data(self,username,josn_data):
		try:
			with open(self.file_url+username+".json",'r') as load_f:
				load_dict = json.load(load_f)
				load_dict['project'].append(josn_data)
				with open(self.file_url+username+".json","w") as f:
					json.dump(load_dict,f)
		except:
			self.crate_file(username)  # 当没有该用户文件时创建一个
			with open(self.file_url+username+".json",'r') as load_f:
				load_dict = json.load(load_f)
				load_dict['project'].append(josn_data)
				with open(self.file_url+username+".json","w") as f:
					json.dump(load_dict,f)

	def del_data(self,username,plan_title):
		with open(self.file_url+username+".json",'r') as load_f:
			load_dict = json.load(load_f)
			project_list = load_dict['project'] 
			for i in project_list:
				if i['item_name'] == plan_title:
					project_list.remove(i)
		with open(self.file_url+username+".json","w") as f:
			json.dump(load_dict,f)

	def serch_data(self,username,plan_title):
		with open(self.file_url+username+".json",'r') as load_f:
			load_dict = json.load(load_f)
			project_list = load_dict['project'] 
			for i in project_list:
				if i['item_name'] == plan_title:
					return i['content']



class store_Json_file():

	if (hostname()) == 'DESKTOP-424AROC':  #开发环境
		file_url = 'C:\\Users\\mayn\\Desktop\\store_manage\\store_data.json'
	else:
		file_url = '/home/Program_manage/store_manage/store_data.json'



	# 添加数据到json文件
	def add_data(self,josn_data):
		# try:
		with open(self.file_url,'r') as load_f:
			load_dict = json.load(load_f)
			load_dict['project'].append(josn_data)
			with open(self.file_url,"w") as f:
				json.dump(load_dict,f)
	
	# # 修改数据
	def update_data(self,id):
		with open(self.file_url,'r') as load_f:
			load_dict = json.load(load_f)
			load_dict['project'][id - 1]['is_visit'] = True

			with open(self.file_url,"w") as f:
				json.dump(load_dict,f)
			return load_dict['project'][id - 1]





if __name__ == '__main__':

	#创建文件
	# crate_file('yang222')

	# 添加数据
	# json_data = {
	# 	"item_name": "2018年9月第一次计划",
	# 	"content": [{
	# 		"item": "30岁 120-300 女"
	# 	}, {
	# 		"item": "40岁 120-300 女"
	# 	}]
	# }
	# add_data('yang111.json',json_data)

	# 删除数据
	# del_data('yang111.json','2018年9月第一次计划')

	#查找数据
	json_class = store_Json_file()
	print (json_class.update_data(1))











# new_dict = {
#     "username": "yang2",
#    "project": [
#      {"item_name": "2018年9月第一次计划",
#        "content": [
#          {"item": "30岁 120-300 女"},
#          {"item": "40岁 120-300 女"},
#          {"item": "30岁 120-300 女"},
#          {"item": "30岁 120-300 女"},
#          {"item": "50岁 120-300 女"},
#          {"item": "30岁 120-300 女"},
#          {"item": "30岁 120-300 女"},
#          {"item": "60岁 120-300 女"},
#          {"item": "30岁 120-300 女"},
#          {"item": "30岁 120-300 女"},
#          {"item": "30岁 120-300 女"},
#          {"item": "30岁 120-300 女"},
#          {"item": "30岁 120-300 女"}
#        ]},
#      {"item_name": "2018年8月第二次计划",
#        "content": [
#          {"item": "30岁 120-300 女"},
#          {"item": "40岁 120-300 女"},
#          {"item": "30岁 120-300 女"},
#          {"item": "30岁 120-300 女"},
#          {"item": "50岁 120-300 女"},
#          {"item": "30岁 120-300 女"},
#          {"item": "30岁 120-300 女"},
#          {"item": "60岁 120-300 女"},
#          {"item": "30岁 120-300 女"},
#          {"item": "30岁 120-300 女"},
#          {"item": "30岁 120-300 女"},
#          {"item": "30岁 120-300 女"},
#          {"item": "30岁 120-300 女"}
#        ]}
#      ]
#   }


# # 添加数据
# with open("test.json","a") as f:
# 	json.dump(new_dict,f)
# 	print("加载入文件完成...")